package com.example.mytestdemo.customviewbook.two

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext

/**
 * @author tgw
 * @date 2023/2/8
 * @describe 折叠的view
 */
class PolytoPolyViewView5 @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr) {

    var mWidth = 0
    var mHeight = 0
    var sTransHeight = 50f
    var mFoldWidth = 0f
    var spiltNum = 8
    var mMatrix: Array<Matrix> = Array(spiltNum) {
        Matrix()
    }

    //j假设折叠后变为原来的0.8倍
    var mFactory = 0.8f

    var mSolidPaint = Paint()
    var mShadowPaint = Paint()
    lateinit var mLinearGradient: LinearGradient


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 计算子View的大小
        val childCount = childCount
        var maxWidth = 0
        var maxHeight = 0
        for (i in 0 until childCount) {
            val child = getChildAt(i)
            measureChild(child, widthMeasureSpec, heightMeasureSpec)
            maxWidth = Math.max(maxWidth, child.measuredWidth)
            maxHeight = Math.max(maxHeight, child.measuredHeight)
        }

        // 设置自定义View的大小为子View的大小
        setMeasuredDimension(maxWidth, maxHeight)

        MeasureSpec.getMode(widthMeasureSpec)
        MeasureSpec.getSize(widthMeasureSpec)
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h

        mFoldWidth = (width / spiltNum).toFloat()
        var mFoldItemWidth = (mWidth * mFactory / spiltNum).toFloat()
        sTransHeight =
            Math.sqrt((mFoldWidth * mFoldWidth - mFoldItemWidth * mFoldItemWidth).toDouble())
                .toFloat()
        //初始化阴影颜色
        var alpha = (255 * (1 - 0.8)).toInt()
        mSolidPaint.color = Color.parseColor("#ffE6F60A")
        mSolidPaint.alpha = alpha
        mShadowPaint.alpha = alpha
        mLinearGradient = LinearGradient(
            0f,
            0f,
            mFoldWidth,
            0f,
            Color.BLACK,
            Color.TRANSPARENT,
            Shader.TileMode.CLAMP
        )
        mShadowPaint.shader = mLinearGradient


        for (i in 0 until spiltNum) {
            var src = floatArrayOf(
                mFoldWidth * i,
                0f,
                mFoldWidth * (i + 1),
                0f,
                mFoldWidth * (i + 1),
                mHeight.toFloat(),
                mFoldWidth * i,
                mHeight.toFloat()
            )
            var isDown = i % 2 == 0
            var dst = floatArrayOf(
                mFoldItemWidth * i,
                if (isDown) 0f else sTransHeight,

                mFoldItemWidth * (i + 1),
                if (isDown) sTransHeight else 0f,

                mFoldItemWidth * (i + 1),
                mHeight + if (isDown) sTransHeight else 0f,

                mFoldItemWidth * i,
                mHeight + if (isDown) 0f else sTransHeight
            )
            mMatrix[i].setPolyToPoly(src, 0, dst, 0, src.size shr 1)
        }


    }

    override fun dispatchDraw(canvas: Canvas) {

        for (i in 0 until spiltNum) {

            canvas.save()
            val rect = Rect(mFoldWidth.toInt() * i, sTransHeight.toInt(), mFoldWidth.toInt() * (i + 1), height+sTransHeight.toInt());
            canvas.setMatrix(mMatrix[i])
            canvas.clipRect(rect)
            super.dispatchDraw(canvas)


            //画阴影
            canvas.translate(mFoldWidth * i, 0f)

            if (i % 2 == 0) {
                canvas.drawRect(0f, 0f, mFoldWidth, mHeight.toFloat(), mSolidPaint)
            } else {
                canvas.drawRect(0f, 0f, mFoldWidth, mHeight.toFloat(), mShadowPaint)

            }

            canvas.restore()
        }
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return super.onInterceptTouchEvent(ev)
    }

}

suspend fun View.getTgw(context: CoroutineContext, block: suspend CoroutineScope.() -> Unit){

    block
}
